package com.xingongchang.baolebei.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.external.activeandroid.util.ReflectionUtils;
import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.ToastView;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.AddressModel;
import com.xingongchang.baolebei.model.ProtocolConst;

public class NewAddressActivity extends BaseActivity implements
		BusinessResponse
{

	private TextView title;
	private ImageView back;

	private EditText name;
	private EditText tel;
	// private EditText email;
	private EditText zipCode;
	private LinearLayout area;
	private TextView address;
	private EditText detail;
	private FrameLayout add;

	private String country_id;
	private String province_id;
	private String city_id;
	private String county_id;

	private AddressModel addressModel;
	private int flag;

	private ProgressDialog pd = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_address);

		Intent intent = getIntent();
		flag = intent.getIntExtra("balance", 0);

		title = (TextView) findViewById(R.id.top_view_text);
		Resources resource = (Resources) getBaseContext().getResources();
		title.setText(resource.getString(R.string.address_add));
		back = (ImageView) findViewById(R.id.top_view_back);
		back.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
			}
		});

		name = (EditText) findViewById(R.id.add_address_name);
		tel = (EditText) findViewById(R.id.add_address_telNum);
		// email = (EditText) findViewById(R.id.add_address_email);
		zipCode = (EditText) findViewById(R.id.add_address_zipCode);
		area = (LinearLayout) findViewById(R.id.add_address_area);
		address = (TextView) findViewById(R.id.add_address_address);
		detail = (EditText) findViewById(R.id.add_address_detail);
		add = (FrameLayout) findViewById(R.id.add_address_add);

		area.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(NewAddressActivity.this,
						RegionPickActivity.class);
				startActivityForResult(intent, 1);
				overridePendingTransition(R.anim.my_scale_action,
						R.anim.my_alpha_action);

			}
		});

		add.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				String consignee = name.getText().toString();
				String telNum = tel.getText().toString();
				String zipcode = zipCode.getText().toString();
				String address = detail.getText().toString();

				Resources resource = (Resources) getBaseContext()
						.getResources();
				String name = resource.getString(R.string.add_name);
				String addtel = resource.getString(R.string.add_tel);
				String cor = resource.getString(R.string.add_correct_email);
				String adda = resource.getString(R.string.add_address);
				String con = resource.getString(R.string.confirm_address);

				if ("".equals(consignee))
				{
					ToastView toast = new ToastView(NewAddressActivity.this,
							name);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else if ("".equals(tel.getText().toString().trim()))
				{
					ToastView toast = new ToastView(NewAddressActivity.this,
							addtel);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}else if(isMobileNO(tel.getText().toString().trim())==false){
					ToastView toast = new ToastView(NewAddressActivity.this,
							"手机格式不正确");
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				}else if ("".equals(address))
				{
					ToastView toast = new ToastView(NewAddressActivity.this,
							adda);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else if (country_id == null || province_id == null
						|| city_id == null || county_id == null)
				{
					ToastView toast = new ToastView(NewAddressActivity.this,
							con);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else
				{
					addressModel = new AddressModel(NewAddressActivity.this);
					addressModel.addResponseListener(NewAddressActivity.this);
					
					addressModel.addAddress(consignee,tel.getText().toString().trim(),zipcode,zipcode,
							address, country_id, province_id, city_id,
							county_id);

				}

			}
		});
	}
    //手机号验证
	public boolean isMobileNO(String mobiles)
	{
		Pattern p = Pattern
				.compile("[1][358]\\d{9}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}
    //是否全是数字验证
	public boolean isNumeric(String str)
	{
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches())
		{
			return false;
		}
		return true;

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == 1)
		{
			if (data != null)
			{
				country_id = data.getStringExtra("country_id");
				province_id = data.getStringExtra("province_id");
				city_id = data.getStringExtra("city_id");
				county_id = data.getStringExtra("county_id");

				StringBuffer sbf = new StringBuffer();
				sbf.append(data.getStringExtra("country_name") + " ");
				sbf.append(data.getStringExtra("province_name") + " ");
				sbf.append(data.getStringExtra("city_name") + " ");
				sbf.append(data.getStringExtra("county_name"));
				address.setText(sbf.toString());

			}
		}
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException
	{
		if (url.endsWith(ProtocolConst.ADDRESS_ADD))
		{
			if (flag == 1)
			{
				Intent intent = new Intent();
				intent.putExtra("ok", "ok");
				setResult(Activity.RESULT_OK, intent);
				finish();
			} else
			{
				finish();
			}

		}

	}

}
